#include "resourcerendertarget.h"
#include "resourcemanager.h"
#include "../defines.h"
#include "resourcewindow.h"
#include <d3d9.h>

namespace gunsmith
{
CResourceRenderTarget::CResourceRenderTarget() : CResource(RT_RENDERTARGET)
{
	m_pSurface	= NULL;
	m_pTexture	= NULL;	
}

CResourceRenderTarget::~CResourceRenderTarget()
{

}

/*!	\brief		Registers the Direct3D device and adds the render surface to resource manager.
	\param		pSource	Should be NULL, otherwise you have to provide your own render surface.
	\return		bool	Returns true on success.
	\remarks	
	\sa			*/
bool CResourceRenderTarget::Create(void *pSource)
{
	if(pSource != NULL)
	{
		m_pSurface = (IDirect3DSurface9*)pSource;
		return true;
	}
	else
	{
		// get window size
		CResourceWindow **ppWindow;
		m_pResMngr->GetResourcesByName("display_window", NULL, (CResource***)&ppWindow);
		UINT iWidth = (*ppWindow)->GetInfo()->iWidth;
		UINT iHeight = (*ppWindow)->GetInfo()->iHeight;
		delete [] ppWindow;

		// if no source is provided create own render target
		if(SUCCEEDED(m_pDevice->CreateTexture(iWidth, iHeight, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &m_pTexture, NULL)))
			return SUCCEEDED(m_pTexture->GetSurfaceLevel(0, &m_pSurface));
	}

	return false;
}

/*!	\brief		Releases surface and texture.
	\return		void
	\remarks	
	\sa			*/
void CResourceRenderTarget::Destroy()
{
	RELEASE(m_pSurface);
	RELEASE(m_pTexture);
}

IDirect3DSurface9* CResourceRenderTarget::GetSurface() const
{
	return m_pSurface;
}

IDirect3DTexture9* CResourceRenderTarget::GetTexture() const
{
	return m_pTexture;
}
};